﻿#include "MessageQueue.h"
#include "Logger.h"

using namespace log4net;

namespace Server
{

MessageQueue *const MessageQueue::instance = new MessageQueue();

    MessageQueue *MessageQueue::getInstance()
    {
        return instance;
    }

    MessageQueue::MessageQueue()
    {
    }

    void MessageQueue::Enqueue(const std::string &msg)
    {
        if (MessageLog->Count < 100)
        {
            MessageLog->Enqueue(StringHelper::formatSimple("[{0}]: {1}" + "\r\n", DateTime::Now, msg));
        }

        Logger::GetLogger(LogType::Server)->Info(msg);
    }

    void MessageQueue::Enqueue(std::runtime_error ex)
    {
        if (MessageLog->Count < 100)
        {
            MessageLog->Enqueue(StringHelper::formatSimple("[{0}]: {1} - {2}" + "\r\n", DateTime::Now, ex.TargetSite, ex));
        }

        Logger::GetLogger(LogType::Server)->Error(ex);
    }

    void MessageQueue::EnqueueDebugging(const std::string &msg)
    {
        if (DebugLog->Count < 100)
        {
            DebugLog->Enqueue(StringHelper::formatSimple("[{0}]: {1}" + "\r\n", DateTime::Now, msg));
        }

        Logger::GetLogger(LogType::Debug)->Debug(msg);
    }

    void MessageQueue::EnqueueChat(const std::string &msg)
    {
        if (ChatLog->Count < 100)
        {
            ChatLog->Enqueue(StringHelper::formatSimple("[{0}]: {1}" + "\r\n", DateTime::Now, msg));
        }

        Logger::GetLogger(LogType::Chat)->Info(msg);
    }
}
